﻿using System;
using System.ComponentModel;
using Notoric.Gui.Windows;

namespace Notoric.Gui.Commands
{
    /// <summary>
    /// Implements a command that inserts a new image section to the active page
    /// presenting an image that the user has selected from the project resources.
    /// </summary>
    /// <remarks>
    /// <para>
    /// The command can execute if the active page is not <see langword="null"/>.
    /// </para>
    /// <para>
    /// The execution proceeds as follows:
    /// <list type="number">
    /// <item>
    /// A <see cref="SelectPictureFromResourcesDialog"/> is showed to the user (filter is set to image files).
    /// </item>
    /// <item>
    /// If the user confirms the dialog the current application state's mouse target 
    /// is set to the active page control and the command to a <see cref="ViewModel.NewImageSectionCommand"/> 
    /// instance whose ImageResource parameter is set to the selected resource from the previous step.
    /// </item>
    /// </list>
    /// </para>
    /// </remarks>
    public class InsertImageSectionFromResourceCommand : RibbonCommand
    {
        #region Constructors

        /// <summary>
        /// Creates a new <see cref="InsertImageSectionFromResourceCommand"/> instance.
        /// </summary>
        /// <param name="mainWindow">
        /// <para>
        /// Type: <see cref="MainWindow"/>
        /// </para>
        /// <para>
        /// Reference to the application's main window.
        /// </para>
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// The <paramref name="mainWindow"/> is <see langword="null"/>.
        /// </exception>
        public InsertImageSectionFromResourceCommand(MainWindow mainWindow)
            : base(mainWindow)
        {
            ScreenTipTitle = "Insert an Image from a Resource";
            ScreenTipText = "Creates an image section in the current page that will display a picture selected from the resources in your project.";
            DisableReason = "A page must be open first.";

            mainWindow.PropertyChanged += new PropertyChangedEventHandler(mainWindow_PropertyChanged);
        }

        #endregion

        #region ICommand Members

        /// <inheritdoc/>
        public override bool CanExecute(object parameter)
        {
            return ApplicationWindow.ActivePage != null;
        }

        /// <inheritdoc/>
        public override void Execute(object parameter)
        {
            var dlg = new SelectPictureFromResourcesDialog();
            dlg.ResourcesRoot = ApplicationWindow.ProjectExplorer.Projects[0].GetRootDirectory(ViewModel.Elements.RootDirectory.Resources);

            if (dlg.ShowDialog() == true)
            {
                if (dlg.SelectedResource != null)
                {
                    var cmd = new ViewModel.NewImageSectionCommand(ApplicationWindow.ActivePage.UndoStack, ApplicationWindow.ActivePage.RedoStack)
                    {
                        Owner = ApplicationWindow.ActivePage,
                        ImageResource = dlg.SelectedResource
                    };

                    var appState = ((App)App.Current).CurrentState;
                    appState.MouseTarget = typeof(Controls.Page);
                    appState.Command = cmd;
                    appState.StatusBarMessage = "Draw the area where you want to put the section.";
                }
            }
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Occurs when a property of the main application window has changed.
        /// </summary>
        /// <param name="sender">Object that has sent the event</param>
        /// <param name="e">Event data</param>
        private void mainWindow_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ActivePage")
                RaiseCanExecuteChanged();
        }

        #endregion
    }
}
